/**
 * @file     maxProfit.js
 * @brief    [121. 买卖股票的最佳时机](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/)
 * @author   Zhu
 * @date     2024-12-03 15:44
 */

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
    let min = Infinity;
    let ans = 0;
    for (let i = 0; i < prices.length; ++i) {
        if (prices[i] > min) {
            const profit = prices[i] - min;
            if (profit > ans) ans = profit;
        } else {
            min = prices[i];
        }
    }

    return ans;
};
